package com.softserve.academy.food.security;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Coding 
{
	private MessageDigest md5; 
	private String type="md5";
	
	public Coding()
	{
	}
	
	public Coding( String type )
	{
		this.type = type;
	}
	
    public String getHash( String str ) 
    {    
    	String hashedPass;
        
        try 
        {
        	md5 = MessageDigest.getInstance( type );  
        	md5.update( str.getBytes(),0, str.length() );  
        	hashedPass = new BigInteger( 1,md5.digest() ).toString(16);  
        	if ( hashedPass.length() < 32 ) 
        	{
        	   hashedPass = "0" + hashedPass;
        	}
        }                                                                  
        catch ( NoSuchAlgorithmException e ) 
        {                        
            return "ErrHash";
        }
        
        return hashedPass;
    }
     
}